
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;


import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;



/**
 * google包test
 * @Class Name JsonTest
 * @Author 005
 * @Create In Aug 27, 2012
 */
public class JsonTest {

	/**
	 * @Methods Name main
	 * @Create In Aug 27, 2012 By 005
	 * @param args void
	 * @throws Exception 
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Gson gson = new Gson();
		
//		JsonObject jsonObject = new JsonObject();
//		jsonObject.addProperty("name", "李康");
//		jsonObject.addProperty("company", "宜搜");
//		System.out.println(jsonObject.toString());
//		Person person = (Person) gson.fromJson(jsonObject, Person.class);
		String jsonString = "{'topn':[-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1]}";
		JsonParser jParser = new JsonParser();
		JsonObject jo = (JsonObject) jParser.parse(jsonString);
		System.out.println(jo.toString());
		JsonArray jsonArray = (JsonArray) jo.get("topn");
		System.out.println(jsonArray.size());
		for (int i = 0; i < jsonArray.size(); i++) {
			System.out.println(jsonArray.get(i).getAsString());
		}
		
		String url = "http://120.197.95.160:8080/intf/regist.jsp?userName=new028&password=123456789&cellPhone=15811037709&contact=%E6%9D%8E&company=%E4%B8%AD%E6%96%87&site=%E7%BD%91%E7%AB%99&url=www.bluesky.com&mail=a@a.c1222&province=3&address=afdajdk%E5%95%8A%E5%93%88%E5%93%88%E5%93%88&post=160000&telcode=010&telephone=1233456&faxcode=010&fax=656565&industry=2&smallindustry=134&token=proxysys";
		JsonElement jsonElement;
		try {
			jsonElement = getJsonObjectByUrl(url);
			System.out.println(jsonElement.toString());
			User user = (User) gson.fromJson(jsonElement, User.class);
			System.out.println(user.getUserId()+":"+user.getSuccess());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
	}
	
	public class User {
		String userId;
		String success;
		public String getUserId() {
			return userId;
		}
		public void setUserId(String userId) {
			this.userId = userId;
		}
		public String getSuccess() {
			return success;
		}
		public void setSuccess(String success) {
			this.success = success;
		}
		
	}
	
	public static JsonElement getJsonObjectByUrl(String url) throws Exception {
		 System.out.println(url);
		 URL accessUrl = new URL(url);
        HttpURLConnection conn = (HttpURLConnection) accessUrl.openConnection();  
        conn.setDoOutput(true);  
        conn.setUseCaches(false);  
        conn.setDefaultUseCaches(false); 
        //设置超时时间为5秒
        conn.setConnectTimeout(5000);  
        conn.getOutputStream();
        conn.connect();  
    	 InputStream inputStream = conn.getInputStream();  
    	 //读回注意编码
		 java.io.BufferedReader reader = new java.io.BufferedReader(new java.io.InputStreamReader(inputStream,"UTF-8"));  
		 String currentLine = "";  
		 String backJsonString = "";  
		 while ((currentLine = reader.readLine()) != null) {
			backJsonString+=currentLine;  
		 }
			JsonParser jsonParser = new JsonParser();
			JsonElement jsonElement = jsonParser.parse(backJsonString);
//		JSONObject backJsonObejct = JSONObject.fromObject(backJsonString);
		return jsonElement;
	}


}
